<?php

class COrderDelay extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	
	public function _setup(){
		
		$this->_name=R_PRE."order_delays";
		
		$this->_primary="orderid";
		
		parent::_setup();
		
	}
	
	public function createOrderDelay($data){
		
		$orderid=$this->insert($data);

		return $orderid;
		
	}
	
	
	public function getDelayDetails($fieldArray=null){
		
		$db=$this->getAdapter();
		
		$whereString=null;
		
		if($fieldArray!=null){
		
			$where=array();
			
			foreach ($fieldArray as $field => $value){
				
				$where[]= $db->quoteInto("$field = ?", $value);
				
			}
			
			$whereString=join(' AND ',$where);
		
		}
		
		$row=$this->fetchAll($whereString);
		
		if($row!=null){
	
			return $row->toArray();
			
       	}else{
       		
       		return array();
       		
       	}
		
	}
	
	public function editDelay($orderid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('orderid = ?', $orderid);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
	}
	
	public function delDelays($orderidArray){
		
		$db=$this->getAdapter();
		
		$orderidString=join(',',$orderidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE orderid IN ($orderidString)");
		
		return $result;
		
	}
	
	
	
	public function getAllDelayCounts($where=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name,'COUNT(*)');
		
		if($where!=''){
			
			$select->where($where);
	
		}

		return $db->fetchOne($select);
		
	}
	
	public function getAllDelays($from,$counts,$where=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		if($where!=''){
			
			$select->where($where);
			
		}
		
		$select->order("$this->_name.add_time DESC");
		
		if($counts!=0){
		
			$select->limit($counts, $from);
		
		}
		
		return $db->fetchAll($select);
		
	}
	

	
}